<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Video
 *
 * @author Administrator
 */
class Video extends CActiveRecord {

    public $video_region;
    public $video_play;
    public $video_details;
    public $video_poster;

    public function rules() {
        return array(
            array('video_name,video_play_money,video_details,video_allow_play,video_allow_down', 'required', 'on' => 'insert'),
            array('category', 'required', 'message' => Yii::t('default', '最少得选择一个分类'), 'on' => 'insert'),
            array('video_region,video_quality,video_play_level,video_dialog,video_player_alias', 'required',
                'message' => Yii::t('default', '请选择...'), 'on' => 'insert'),
            array('video_play_level,video_down_level', 'numerical', 'allowEmpty' => false, 'tooSmall' => Yii::t('default', '请选择权限用户组'), 'min' => 1, 'max' => 10),
            array('video_name,video_alias,video_release', 'length', 'max' => 30, 'on' => 'insert'),
            array('video_directors,video_actors,video_alias', 'length', 'max' => 250, 'on' => 'insert'),
            //array('video_name,video_alias', 'unique'),
            array('video_down_money,video_play_money', 'numerical', 'min' => 0, 'on' => 'insert'),
        );
    }

    public function attributeLabels() {
        return array(
            'video_id' => Yii::t('default', '编号'),
            'video_name' => Yii::t('default', '视频名称'),
            'category' => Yii::t('default', '视频分类'),
            'video_alias' => Yii::t('default', '视频别名'),
            'video_quality' => Yii::t('default', '视频画质'),
            'video_details' => Yii::t('default', '视频介绍'),
            'video_allow_play' => Yii::t('default', '是否允许点播'),
            'video_allow_down' => Yii::t('default', '是否允许下载'),
            'video_play_level' => Yii::t('default', '点播要求权限'),
            'video_down_level' => Yii::t('default', '下载授权'),
            'video_release' => Yii::t('default', '发行时间'),
            'video_region' => Yii::t('default', '出品地区'),
            'video_dialog' => Yii::t('default', '视频对白'),
            'video_directors' => Yii::t('default', '导演'),
            'video_actors' => Yii::t('default', '主演'),
            'video_play' => Yii::t('default', '开放模式'),
            'video_star' => Yii::t('default', '评分'),
            'video_url' => Yii::t('default', '视频地址'),
            'video_title' => Yii::t('default', '视频路径名称'),
            'video_type' => Yii::t('default', '视频模式'),
            'video_create_user' => Yii::t('default', '发布人'),
            'video_create_time' => Yii::t('default', '发布时间'),
            'video_modify_user' => Yii::t('default', '修改人'),
            'video_modify_time' => Yii::t('default', '修改时间'),
            'video_add_title' => Yii::t('default', '发布视频'),
            'video_edit_title' => Yii::t('default', '编辑视频资料'),
            'video_allow_play_label' => Yii::t('default', '允许点播'),
            'video_allow_down_label' => Yii::t('default', '允许下载'),
            'video_server' => Yii::t('default', '视频所在服务器'),
            'video_input_type' => Yii::t('default', '视频路径模式'),
            'video_textarea' => Yii::t('default', '路径列表'),
            'video_play_server' => Yii::t('default', '文件服务器'),
            'video_down_server' => Yii::t('default', '下载服务器'),
            'video_down_money' => Yii::t('default', '下载扣费'),
            'video_play_money' => Yii::t('default', '扣费'),
            'video_thumb' => Yii::t('default', '标准海报'),
            'video_poster' => Yii::t('default', '宽屏海报'),
            'video_dialog_list' => Yii::t('default', '英语,中文字母,国语,粤语,韩语,日语,法语,西班牙'),
            'video_dialog_region' => Yii::t('default', '大陆,香港,台湾,韩国,日本,美国,法国,其他'),
            'video_player_alias' => Yii::t('default', '视频播放器'),
            'video_hots' => Yii::t('default', '热度'),
            'video_daliy_hots' => Yii::t('default', '日热度'),
            'video_weekly_hots' => Yii::t('default', '周热度'),
            'video_monthly_hots' => Yii::t('default', '月热度'),
            'categorys' => Yii::t('default', '视频分类'),
            'servers' => Yii::t('default', '服务器'),
            'videoLevel' => Yii::t('default', '视频权限')
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{video}}';
    }

    public function getCategoryList($values = array()) {

        $values = is_array($values) ? $values : array();
        $result = Category::model()->findAll('category_parent_id=0');
        $html = '';
        foreach ($result as $value) {
            if ($value['category_children_num'] > 0) {
                $html.= '<li class="title">' . $value['category_name'] . '</li>' . $this->getChildrenCategory($value['category_id'], $values);
            } else {
                $options = array('value' => $value['category_id'], 'id' => 'category_' . $value['category_id']);
                $html.= '<li class="title">' . CHtml::checkBox('Video[category][]', in_array($value['category_id'], $values), $options) . CHtml::label($value['category_name'], 'category_' . $value['category_id']) . '</li>';
            }
        }
        return $html;
    }

    protected function getChildrenCategory($parentId, $values = array()) {
        $result = Category::model()->findAll('category_parent_id=:id', array(':id' => $parentId));
        if ($result) {
            $html = "";
            foreach ($result as $value) {
                $options = array('value' => $value['category_id'], 'id' => 'category_' . $value['category_id']);
                $html.= '<li>' . CHtml::checkBox('Video[category][]', in_array($value['category_id'], (array) $values), $options) . CHtml::label($value['category_name'], 'category_' . $value['category_id']) . '</li>';
            }
            return $html;
        } else {
            return null;
        }
    }

    public function getPlayArray() {
        return array(Yii::t('default', '允许'), Yii::t('default', '关闭'));
    }

    /**
     * 视频质量
     * @param type $key
     * @return string 
     */
    public function getQualityData($key = null) {
        $str = Yii::t('default', 'DVD, VCD, TC, 高清');
        $list = explode(',', $str);
        $result = array();
        foreach ($list as $name) {
            $result[] = array('name' => $name);
        }
        return CHtml::listData($result, 'name', 'name');
    }

    public function getDialogArray() {
        $list = explode(',', $this->getAttributeLabel('video_dialog_list'));
        $result = array();
        foreach ($list as $name) {
            $result[] = array('name' => $name);
        }
        return CHtml::listData($result, 'name', 'name');
    }

    /**
     * 编辑的时候用
     */
    public function getCategory() {
        $category = new VideoCategorys();
        $list = $category->findAll('video_id=:id', array(':id' => $this->video_id));
        if ($list) {
            foreach ($list as $var) {
                $this->category[] = $var['category_id'];
            }
        }
        return $this->category;
    }

    public function setCategory($category) {
        $this->category = $category;
    }

    public function getUserGroup() {
        $models = UserGroup::model()->findAll();
        $data = CHtml::listData($models, 'usergroup_level', 'usergroup_name');
        return array_merge_recursive(array('0' => '请选择权限'), $data);
    }

    public function getPlayer() {
        $result = Player::model()->findAll();
        return CHtml::listData($result, 'player_alias', 'player_name');
    }

    public function getRegionArray() {
        $list = explode(',', $this->getAttributeLabel('video_dialog_region'));
        $result = array();
        foreach ($list as $name) {
            $result[] = array('name' => $name);
        }
        return CHtml::listData($result, 'name', 'name');
    }

    public function afterSave() {
        if (is_array($this->video_region)) {
            VideoRegion::model()->deleteAll('video_id=:id', array(':id' => $this->video_id));
            foreach ($this->video_region as $regionName) {
                $region = new VideoRegion();
                $region->video_id = $this->video_id;
                $region->region_name = $regionName;
                if (!$region->save()) {
                    throw new Exception('save region error');
                }
            }
        }
        if (is_array($this->category)) {
            VideoCategorys::model()->deleteAll('video_id=:id', array(':id' => $this->video_id));
            foreach ($this->category as $categoryId) {
                $categorys = new VideoCategorys();
                $categorys->category_id = $categoryId;
                $categorys->video_id = $this->video_id;
                if (!$categorys->save()) {
                    throw new Exception('save category error');
                }
            }
        }
    }
    /**
     *视频保存之前
     * @return boolean 
     */
    public function beforeSave() {
        if (parent::beforeSave()) {
            $this->video_dialog = implode(',', $this->video_dialog);

            if (is_array($this->video_player_alias) && count($this->video_player_alias) > 0) {
                $this->video_player_alias = implode(',', $this->video_player_alias);
            }
            if ($this->isNewRecord) {
                $this->video_create_user = Yii::app()->cookies->admin;
                $this->video_create_time = time();
                $this->video_modify_time = time();
                $this->video_modify_user = Yii::app()->cookies->admin;
            } else {
                $this->video_modify_time = time();
                $this->video_modify_user = Yii::app()->cookies->admin;
            }
            return true;
        } else {
            return false;
        }
    }

    public function getVideoCategoryLink($vid) {
        $category = new VideoCategorys();
        $result = $category->findAll('video_id=:id', array(':id' => $vid));
        $data = array();
        foreach ($result as $var) {
            $data[$var['category_id']] = Category::model()->findByPk($var->category_id)->category_name;
        }
        return implode(',', $data);
    }

    /**
     * 根据视频的权限发挥权限组名称
     * @param type $playLevelId
     * @param type $downLevelId
     * @return type 
     */
    public function getVideoLevel() {
        if ($this->video_play_level == $this->video_down_level) {
            $ugroup = UserGroup::model()->find('usergroup_level=:id', array(':id' => $this->video_play_level));
            if ($ugroup) {
                return $ugroup->usergroup_name;
            } else {
                return 'undefined';
            }
        } else {
            $result = '';
            $usergroup = new UserGroup();
            $ugroup = $usergroup->find('usergroup_level=:id', array(':id' => $this->video_play_level));
            if ($ugroup) {
                $result.= $ugroup->usergroup_name;
            } else {
                $result.= 'undefined';
            }
            $ugroup = $usergroup->find('usergroup_level=:id', array(':id' => $this->video_down_level));
            if ($ugroup) {
                $result.= '<br />'.$ugroup->usergroup_name;
            } else {
                $result.= '<br />undefined';
            }
            return $result;
        }
    }

    /**
     * 视频修改方法
     * @param int $pk
     * @return object
     */
    public function getEditDatabyPk($pk) {
        $result = $this->findByPk($pk);
        $regionObject = VideoRegion::model()->findAll('video_id=:id', array(':id' => $pk));
        $region = array();
        $regionData = $this->getRegionArray();
        foreach ($regionObject as $row) {
            $region[] = array_search($row['region_name'], $regionData);
        }
        if ($result['video_dialog']) {
            $result['video_dialog'] = array_intersect(explode(',', $result['video_dialog']), $this->getDialogArray());
            if (is_array($result['video_dialog'])) {
                $result['video_dialog'] = array_keys($result['video_dialog']);
            }
        }
        $details = new VideoDetails();
        $detailsRow = $details->findByPk($pk);
        $result['video_play'] = array((int) $result['video_allow_play'] ? 0 : 1, (int) $result['video_allow_down'] ? 1 : 0);
        $result['video_region'] = $region;
        $result['video_play_server'] = '';
        $result['video_details'] = $detailsRow['video_details'];
        $result['video_quality'] = $this->getQualityData($result['video_quality']);
        $result['playStart'] = VideolFile::model()->count('video_id=' . $pk) + 1;
        $result['playEnd'] = $result['playStart'] + 1;
        $result['files'] = VideolFile::model()->findAll('video_id=' . $pk);
        return $result;
    }

    public function getCategorys() {
        $category = new VideoCategorys();
        $name = '';
        foreach ($category->findAll('video_id=:id', array(':id' => $this->video_id)) as $res) {
            $name.= $name ? '&nbsp;|&nbsp;' : '';
            $name.= CHtml::link($res->category->category_name);
        }
        return $name;
    }

    /**
     * 视频所在服务器
     * @return type 
     */
    public function getServers() {
        $server = new VideoServerId();
        $name = '';
        foreach ($server->findAll('video_id=:id', array(':id' => (int) $this->video_id)) as $res) {
            $name.= $name ? '&nbsp;|&nbsp;' : '';
            $name.= CHtml::link($res->server->server_name);
        }
        return $name;
    }

    public function getRegion() {
        $region = new VideoRegion();
        $list = $region->findAll('video_id=:id', array(':id' => $this->video_id));
        $data = array();
        if (is_array($list)) {
            foreach ($list as $var) {
                $data[] = $var['region_name'];
            }
        }
        return $data;
    }

    public function getVideo_hots() {
        return $this->video_daliy_hots . '|' . $this->video_weekly_hots . '|' . $this->video_monthly_hots;
    }
    public function beforeDelete() {
        if(parent::beforeDelete()){
             
        }
        return true;
    }
    public function relations() {
        return array(
            'videoCateroy' => array(self::BELONGS_TO, 'VideoCategorys', 'video_id'),
            'videoServer' => array(self::BELONGS_TO, 'VideoServerId', 'video_id'),
            'videoRegion' => array(self::BELONGS_TO, 'VideoRegion', 'video_id')
        );
    }

}

;